package edu.ustb.mis.dm.impl.algorithm;

import java.util.List;
import java.util.Map;
import java.util.Set;
import edu.ustb.mis.dm.impl.data.TextInputDataPrepare;
import edu.ustb.mis.dm.interfaces.algorithm.ClusterAlgorithm;
import edu.ustb.mis.dm.model.WClusterResult;
import edu.ustb.mis.dm.util.WeightUtils;

public class PWCABOSFV extends AbstractWCABOSFV implements ClusterAlgorithm<WClusterResult> {
	private final Map<Integer, Double> weightMap;

	public PWCABOSFV(final String data_set_name) {
		this.dataPrepare = new TextInputDataPrepare(data_set_name);
		weightMap = WeightUtils.weight(data_set_name);
	}

	@Override
	protected float calculateDissimilarity(final int j) {
		wClusterResult = wClusterResultList.get(j);
		tmp_WClusterResult.clearSet();
		tmp_WClusterResult.init(wClusterResult.getSameSet(), wClusterResult.getAllSet());
		tmp_WClusterResult.contrast(instance.getAttributeSet());
		return 1 - (sumWeight(tmp_WClusterResult.getSameSet()) / sumWeight(tmp_WClusterResult.getAllSet()));
	}

	public void execute() {
		instanceList = dataPrepare.initData();
		final List<WClusterResult> crList = cluster();
		dataOutput.writefile(crList);
	}

	private float sumWeight(final Set<Integer> keySet) {
		float value = 0f;
		for (final Integer key : keySet) {
			value += weightMap.get(key);
		}
		return value;
	}
}
